<?php
namespace SinoPHP\BbdBoss\Model;

use Upfor\UpforPHP\Model;
use SinoPHP\BbdBoss\Library\Dnspod;


/**
* 平台域名线路与dnspod对应表
*/

class  PlatDomainSubRecord extends  Model{

    /*
     *初始化操作
     * */

    public function __construct() {
        parent::__construct();
    }

    /**
     * 更新dnspod记录
     *
     * @param string $action
     * @param int    $plat_domain_line_id
     * @param array  $data
     * @return boolean
     */
    public function updateSubrecord($action, $plat_domain_line_id, $data) {
        if ( !$plat_domain_line_id || !$data) {
            return false;
        }

        $time = time();
        $dnspodDeleteList = [];
        $dnspodObj = new Dnspod("outer");

        $today = date('Y-m-d');
        $logDir = LOG_DIR . "/script_log";
        if (!file_exists($logDir)) {
            @mkdir($logDir, 0777, ture);
        }

        //dnspod删除旧记录
        $recordList = $this->getList('sub_record_id, dnspod_id', ['plat_domain_line_id'=>$plat_domain_line_id]);
        if ($recordList) {
            foreach ($recordList as $value) {
                $dnspodDeleteList[$value['sub_record_id'].'_'.$value['dnspod_id']] = ['recordId'=>$value['dnspod_id']];
            }
            $ret = $dnspodObj->deleteRecordMulti($dnspodDeleteList);
            if ( !isset($ret['success']) || !$ret['success']) {
                return false;
            }
            foreach ($ret['success'] as $ret_k => $ret_v) {
                list($sub_record_id, $recordId) = explode('_', $ret_k);
                if ($ret_v['status']['code'] != 1) {
                    file_put_contents("{$logDir}/{$today}dnspod.log", 'deleteRecordMulti:Fail -> sub_record_id:'.$sub_record_id.' code:'.$ret_v['status']['code'].' msg:'.$dnspodObj->getDnspodCodeMsg($ret_v['status']['code']), FILE_APPEND);
                    continue;
                }
                $recordLog = [
                    'dnspod_id' => $recordId,
                    'type' => 5,
                    'detail' => @json_encode(['action' => $action.'_deleteRecordMulti'] + $ret_v),
                    'create_time' => date('Y-m-d H:i:s', $time),
                ];
                model('domainDnspodRecordLog')->insert($recordLog);
            }
        }

        //dnspod增加新纪录
        $ret = $dnspodObj->addRecordMulti($data);
        if ( !isset($ret['success']) || !$ret['success']) {
            return false;
        }

        $this->delete(['plat_domain_line_id'=>$plat_domain_line_id]);

        foreach ($ret['success'] as $ip => $ret_v) {
            if ($ret_v['status']['code'] != 1 || empty($ret_v['record']['id'])) {
                file_put_contents("{$logDir}/{$today}dnspod.log", 'addRecordMulti:Fail -> plat_domain_line_id:'.$plat_domain_line_id.' ip:'.$ip.' code:'.$ret_v['status']['code'].' msg:'.$dnspodObj->getDnspodCodeMsg($ret_v['status']['code']), FILE_APPEND);
                continue;
            }

            $dnspod_id = $ret_v['record']['id'];
            $recordLog = [
                'dnspod_id' => $dnspod_id,
                'type' => 5,
                'detail' => @json_encode(['action' => $action.'_addRecordMulti'] + $ret_v),
                'create_time' => date('Y-m-d H:i:s', $time),
            ];
            model('domainDnspodRecordLog')->insert($recordLog);

            $info = array(
                'plat_domain_line_id' => $plat_domain_line_id,
                'record' => $ip,
                'dnspod_id' => $dnspod_id,
                'status' => 1,
                'create_time' => $time,
                'update_time' => $time,
                );
            $subRecordId = $this->insert($info);
            if ( !$subRecordId ) {
                file_put_contents("{$logDir}/{$today}dnspod.log", 'PlatDomainSubRecord:add:Fail -> info:'.json_encode($info), FILE_APPEND);
                $dnspodObj->deleteRecord(['recordId' => $dnspod_id]);
            }
        }
        
    }
    /**
     * 删除dnspod记录
     *
     * @param string $action
     * @param int    $plat_domain_line_id
     * @return boolean
     */
    public function deleteSubrecord($action, $plat_domain_line_id) {
        if ( !$plat_domain_line_id ) {
            return false;
        }

        $time = time();
        $dnspodDeleteList = [];
        $dnspodObj = new Dnspod("outer");

        $today = date('Y-m-d');
        $logDir = LOG_DIR . "/script_log";
        if (!file_exists($logDir)) {
            @mkdir($logDir, 0777, true);
        }

        //dnspod删除旧记录
        $recordList = $this->getList('sub_record_id, dnspod_id', ['plat_domain_line_id'=>$plat_domain_line_id]);
        if ($recordList) {
            foreach ($recordList as $value) {
                $dnspodDeleteList[$value['sub_record_id'].'_'.$value['dnspod_id']] = ['recordId'=>$value['dnspod_id']];
            }
            $ret = $dnspodObj->deleteRecordMulti($dnspodDeleteList);
            if ( !isset($ret['success']) || !$ret['success']) {
                return false;
            }
            foreach ($ret['success'] as $ret_k => $ret_v) {
                list($sub_record_id, $recordId) = explode('_', $ret_k);
                if ($ret_v['status']['code'] != 1) {
                    file_put_contents("{$logDir}/{$today}dnspod.log", 'deleteRecordMulti:Fail -> sub_record_id:'.$sub_record_id.' code:'.$ret_v['status']['code'].' msg:'.$dnspodObj->getDnspodCodeMsg($ret_v['status']['code']), FILE_APPEND);
                    continue;
                }
                $recordLog = [
                    'dnspod_id' => $recordId,
                    'type' => 5,
                    'detail' => @json_encode(['action' => $action.'_deleteRecordMulti'] + $ret_v),
                    'create_time' => date('Y-m-d H:i:s', $time),
                ];
                model('domainDnspodRecordLog')->insert($recordLog);
            }
        }

        $this->delete(['plat_domain_line_id'=>$plat_domain_line_id]);
    }

}
